package com.wowjoy.dp.sds.common;

import lombok.Data;
import lombok.NoArgsConstructor;

@Data
@NoArgsConstructor
public class RestResult<T> {

    public static final RestResult<Object> SUCCESS = new RestResult<>(1, "success");
    public static final RestResult<Object> ERROR = new RestResult<>(0, "error");

    private int code;
    private String message;
    private Object context;

    public RestResult(int code, String message) {
        this(code, message, null);
    }
    public RestResult(int code, String message, T context) {
        this.code = code;
        this.message = message;
        this.context = context;
    }

    public void setContext(T context) {
        this.context = context;
    }
    public RestResult<T> copyThis() {
        return new RestResult<>(code, message, null);
    }

    public static RestResult<Object> error(String error) {
        return new RestResult<>(ERROR.code, error);
    }

    @SuppressWarnings("unchecked")
    public static <T> RestResult<T> success(T ctx) {
        RestResult r = RestResult.SUCCESS.copyThis();
        r.setContext(ctx);
        return r;
    }
}
